package com.zyl.basis.state;

/**
 * @author zhaoyl
 * @date 2024/5/27 15:48
 * @description 说明
 * yield 将线程从运行转为就绪（暂停但不阻塞）
 * 礼让不一定成功，看CPU心情
 */
public class TestYield {

    public static void main(String[] args) {
        MyYield myYield = new MyYield();
        new Thread(myYield,"线程A").start();
        new Thread(myYield,"线程B").start();
    }
}

class MyYield implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "线程开始执行");
        Thread.yield();//礼让
        System.out.println(Thread.currentThread().getName() + "线程停止执行");
    }
}